﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace DingFan
{
    public partial class DingFan : Form
    {
        DataAccess DA;
        public DingFan()
        {
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            DateTime date = Convert.ToDateTime(dtpDate.Text);
            bool isLunch = false;
            if (cbIsLunch.SelectedIndex == 0)
            {
                isLunch = true;
            }
            decimal price = 0;
            try
            {
                price = Convert.ToDecimal(txtPrice.Text.Trim());
            }
            catch (Exception ex)
            {
                price = 0;
            }
            string remark = txtRemark.Text.Trim();
            try
            {
                object[] vals = { date, isLunch, price, remark, "Insert" };
                DA.ExecSProc("DingFan_InUpDelSel", vals);
                ShowDataInListView(listV);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DingFan_Load(object sender, EventArgs e)
        {
            string serverPath = @"10.0.4.101\sqlexpressr2";
            string catalog = "dingfan";
            string userID ="sa";
            string password ="qwe123!@#";

            DA = new DataAccess(serverPath, catalog, userID, password);

            cbIsLunch.SelectedIndex = 0;

            ShowDataInListView(listV);
        }
        private void ShowDataInListView(ListView lst)
        {
            DataTable dt = new DataTable();
            dt = DA.ExecSProcDT("DingFan_InUpDelSel", "", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, "Select");
            lst.Clear();
            lst.View = System.Windows.Forms.View.Details;
            lst.AllowColumnReorder = true;
            lst.GridLines = true;

            int RowCount, ColCount, i, j;
            DataRow dr = null;

            if (dt == null) return;
            RowCount = dt.Rows.Count;
            ColCount = dt.Columns.Count;

            for (i = 0; i < ColCount; i++)
            {
                lst.Columns.Add(dt.Columns[i].Caption.Trim(), lst.Width / ColCount, HorizontalAlignment.Left);
            }

            if (RowCount == 0) return;
            for (i = 0; i < RowCount; i++)
            {
                dr = dt.Rows[i];
                lst.Items.Add(dr[0].ToString().Trim());
                for (j = 1; j < ColCount; j++)
                {
                    lst.Items[i].SubItems.Add((string)dr[j].ToString().Trim());
                }
            }
        }
    }
}
